<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex;
		var tempid;
		var tempnum = 1;
		var tempbarcode = new Array();
		$(function() {
			var rid = $('#orderForm').find('input[name=rid]');
			var ridCombobox = rid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				onLoadSuccess : function() {
					if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
						rid.combobox('setValue','${sessionInfo.repertoryIds}');
					}
					parent.$.messager.progress('close');
				},
			    onSelect : function(record){
				    if(record.id!=null){
				    	$.ajax({
							url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
							data : {type : 'RK',rid : record.id},
							type : "POST",
							success : function(d){
								$('#serial').val(d);
							}
						});
				    }
				 }
			});
			var uid = $('#orderForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				onLoadSuccess : function() {
					uid.combobox('setValue','${sessionInfo.userId}');
					parent.$.messager.progress('close');
				}
			});
		
			var date = new Date();
			var month = date.getMonth()+1;
			$("#orderDate").val(date.getFullYear()+"-"+month+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes());
			$('#orderDate').datetimebox({  
		    showSeconds:false  
			});	
			
			if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
				$.ajax({
					url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
					data : {type : 'RK',rid : ${sessionInfo.repertoryIds}},
					type : "POST",
					success : function(d){
						$('#serial').val(d);
					}
				});
			} 
			$('#dg').datagrid({
// 				ReadOnly : true,
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
					}
				},'-',{
					text: '删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelected');
						if (row){
							var index = $('#dg').datagrid('getRowIndex', row);
							$('#dg').datagrid('deleteRow', index);
						}
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						
						var items = [];
						var rows = $('#dg').datagrid('getRows');
						for ( var i = 0; i < rows.length; i++) {
							items.push(rows[i].id);
							items.push(rows[i].num);
							items.push(rows[i].buyprice)
						}
						//alert(items.join(','));
						
						$('#items').val(items.join(','));
						
						/*
						document.orderForm.submit();
						错误原因，Action中无法取到items 的值
						if(1==1)return;
						if(1==1){
							
							document.location.href='${pageContext.request.contextPath}/rkgl/orderAction!add.html?items='+$('#items').val();
							return;
						}
						*/
						
						$('#orderForm').form('submit', {
							url : '${pageContext.request.contextPath}/rkgl/orderAction!add.html?items='+$('#items').val(),
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									parent.showTab("添加入库单","进货管理");
								}
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
// 					 this.Rows[1].Frozen=true;
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
				}
			  
			});
			
			$(".barcode").live("keypress",function(e) {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					_search();
					return false;
				}
				
			});
		});
	
		function _search() {
			$('#dg').datagrid('endEdit', lastIndex);
			var rid = $('#orderForm').find('input[name=rid]').val();
			//$(".rid").attr("name","rid");
			//$(".rid").attr("value",rid);
			$("#mytable").datagrid('loadData',{total:0,rows:[]});					
							
			var lindex=0;
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_search.html',
				success : function(d) {
					var json = $.parseJSON(d);
					if (json.success==null) {
					if(json.length>1){
						$('#mydialog').show(); 
						$('#mydialog').dialog({ 
						collapsible: true, 
						minimizable: false, 
						maximizable: false,
						closable:true,
						closed:false
						}); 

						$("#mytable").datagrid({
							toolbar:[{
								text:'确定',
								iconCls:'icon-save',
								handler:function(){
									var selections=$('#mytable').datagrid('getSelections');
									try{
								
										for(var i=0;i<selections.length;i++){
											var obj=selections[i];
											var index=$("#mytable").datagrid("getRowIndex",obj);
											//alert(index);
											//添加数据
											searchAddGoods(json,index);
										}

										//关闭
										$('#mydialog').dialog({
											closed:true,
										 });
										
										//searchSelectAddGoods(json,jsonid,num)
									
									}catch(e){
										alert(e);
									}
									
								}
							},{
								text:"取消选择",
								iconCls:'icon-undo',
								handler:function(){
									$("#mytable").datagrid("unselectAll");
								}
							}]
							});
						
						for(var i=0;i<json.length;i++){
							 $("#mytable").datagrid('appendRow',{
							 
					            barcode:json[i].barcode,
					            type:json[i].type,
					            color:json[i].color,
					            size:json[i].size,
					            num:0
						     });
						}
						
						$("#mytable").datagrid({
							
							onDblClickRow:function(rowIndex, rowData){
								searchAddGoods(json,rowIndex);
								/*
								$("#mytable").datagrid('loadData',{total:0,rows:[]});					
								$('#mydialog').dialog({
									closed:true,
								 });
								 $("#barcode").val("");
								$("#barcode").focus();
								*/
							}
				
						});


						
						
					}else{
						searchAddGoods(json,0);
						$("#barcode").val("");
						$("#barcode").focus();
					}
					
				    				    
					}else{
						parent.sy.messagerShow({
							msg : json.msg,
							title : '提示'
						});
					}
				}
			});
			$(".rid").attr("name","");
			lastIndex = $('#dg').datagrid('getRows').length-1;
			$('#dg').datagrid('selectRow', lastIndex);
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
		
		function searchAddGoods(json,jsonid){
				    	var rows = $('#dg').datagrid('getRows');
				    	var flag = 0;	
					    if(rows.length > 0){
					        for(var i = 0; i < rows.length; i++) {
					            var tempbarcode = json[jsonid].barcode;
					            if(tempbarcode == rows[i].barcode){	
					                tempnum = rows[i].num + 1;		   
					                $('#dg').datagrid('updateRow',{
	                                    index: i,
	                                    row: {
		                                      num: tempnum
	                                    }
	                                   });					                
					                flag = 1;
					                tempnum = 1;
					                return false;
					            }					            
						    }					        
					    }
					    if(flag == 0){
					         $('#dg').datagrid('appendRow',{
					            id:json[jsonid].id,
					            barcode:json[jsonid].barcode,
							    type:json[jsonid].type,
							    gtname:json[jsonid].gtname,
							    bname:json[jsonid].bname,
							    name:json[jsonid].name,
							    unit:json[jsonid].unit,
							    color:json[jsonid].color,
							    size:json[jsonid].size,
							    //num:'1'
							    buyprice:json[jsonid].buyPrice,
							    num:tempnum
						     });
					    }
					    return false;
	}

	//添加搜索的		
	function searchSelectAddGoods(json,jsonid,num){
	    	var rows = $('#dg').datagrid('getRows');
	    	var flag = 0;	
		    if(rows.length > 0){
		        for(var i = 0; i < rows.length; i++) {
		            var tempbarcode = json[jsonid].barcode;
		            if(tempbarcode == rows[i].barcode){	
		                tempnum = rows[i].num + 1;		   
		                $('#dg').datagrid('updateRow',{
                            index: i,
                            row: {
                                  num: tempnum
                            }
                           });					                
		                flag = 1;
		                tempnum = 1;
		                return false;
		            }					            
			    }					        
		    }
		    if(flag == 0){
		         $('#dg').datagrid('appendRow',{
		            id:json[jsonid].id,
		            barcode:json[jsonid].barcode,
				    type:json[jsonid].type,
				    gtname:json[jsonid].gtname,
				    bname:json[jsonid].bname,
				    name:json[jsonid].name,
				    unit:json[jsonid].unit,
				    color:json[jsonid].color,
				    size:json[jsonid].size,
				    //num:'1'
				    buyprice:json[jsonid].buyPrice,
				    num:num
			     });
		    }
		    return false;
	   }
				   	
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',border:false" style="height: 135px; overflow: hidden;">
			<form id="orderForm" style="height:95px;">
				<input id="items" name="items" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日&nbsp;&nbsp;&nbsp;&nbsp;期</th>
						<td style="width: 200px;">
<!--							<input name="date" class="easyui-validatebox" value="" id="orderDate" data-options="editable:false" style="width: 200px;" />-->
							<input id="orderDate" name='date'  onClick="WdatePicker()" class='easyui-validatebox' style="width: 200px;" data-options="required:'true',missingMessage:'请选择销售时间'"/>
						</td>
						<th style="width: 80px;">单&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" id="serial" style="width: 200px;" value=""/></td>
					</tr>
					<tr>
						<th style="width: 80px;">入库仓库 </th>
						<td style="width: 200px;"><input name="rid" style="width: 200px;"  data-options="required:'true',missingMessage:'请选择入库仓库'"/></td>
						<th style="width: 80px;">操作员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;"/></td>
					</tr>
					<tr>
						<th style="width: 80px;">备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="3"><textarea name="remark" style="height:30px;width: 725px;"></textarea></td>
					</tr>
				</table>
			</form>
			<form id="searchForm" style="height:10px;margin-top:10px;">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 50px;">商品条码</th>
						<td style="width: 200px;"><input class="barcode" id="barcode" name="barcode" style="width: 200px;" /></td>
						<th style="width: 50px;">商品款号</th>
						<td style="width: 200px;"><input class="barcode" id="type" name="type" style="width: 200px;" /></td>
						<td style="width: 280px;" align="right">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	
		<div data-options="region:'center',border:false" >
<!--		<div data-options="region:'center',border:false" style="overflow: hidden;">-->
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true"> 
		        <thead>
		            <tr>
		            	<th data-options="field:'id',width:100,hidden:true">商品ID</th> 
		            	<th data-options="field:'barcode',width:130">商品条码</th>  
		            	<th data-options="field:'type',width:90">款号</th>  
	                 	<th data-options="field:'name',width:100">商品名称</th> 
	                 	<th data-options="field:'bname',width:75">品牌</th>
		                <th data-options="field:'gtname',width:80,align:'center'">商品类别</th>
                   		<th data-options="field:'color',width:70,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:60,align:'center'">尺码</th>
                   		<th data-options="field:'unit',width:50,align:'center'">单位</th>
                   		<th data-options="field:'buyprice',width:85,align:'center',editor:'text'">进价</th>
		                <th data-options="field:'num',width:80,align:'center',editor:'numberbox'">入库数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
	<div id="mydialog" style="display:none;padding:0px;width:410px;height:auto;min-height:300px;max-height:300px;overflow-y:scrooll;" title="该条码对应了多件商品，请双击您要调拨的商品。(可多选)">		 		
		<table id="mytable" class="easyui-datagrid" style="height:auto"  data-options="iconCls:'icon-edit'"> 
		        <thead>
		            <tr>
		            	<th data-options="field:'ck',width:100,checkbox:true">选择</th>
		            	<th data-options="field:'barcode',width:100">商品条码</th>  
		            	<th data-options="field:'type',width:100">款号</th>  
	                 	<!-- <th data-options="field:'name',width:100">商品名称</th> 
	                 	<th data-options="field:'bname',width:80">品牌</th> -->
                   		<th data-options="field:'color',width:80,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:80,align:'center'">尺码</th>
<%--                   		<th data-options="field:'num',width:80,align:'center',editor:'numberbox'">入库数量</th>--%>
		            </tr>
		        </thead>
		    </table>
		</div> 
	</body>
</html>